package com.jweb.framework.ws.soap.axiom;

import org.apache.axiom.om.OMException;
import org.apache.axiom.soap.SOAPBody;
import org.apache.axiom.soap.SOAPEnvelope;
import org.apache.axiom.soap.SOAPFactory;
import org.apache.axiom.soap.SOAPHeader;
import org.springframework.ws.soap.SoapBody;
import org.springframework.ws.soap.SoapEnvelope;
import org.springframework.ws.soap.SoapHeader;
/**
 * 修改人： Thomas
 * 修改内容：新增 
 * 类说明：
 */
class AxiomSoapEnvelope extends AxiomSoapElement implements SoapEnvelope {
	
	boolean payloadCaching ;
	
	private AxiomSoapBody body ;
	
	private final boolean langAttributeOnSoap11FaultString ;
	// "axiomEnvelope"=<?xml version="1.0" encoding="utf-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body></soapenv:Body></soapenv:Envelope>
	AxiomSoapEnvelope(SOAPEnvelope axiomEnvelope , SOAPFactory axiomFactory , boolean payloadCaching , boolean langAttributeOnSoap11FaultString){
		super(axiomEnvelope, axiomFactory);
		this.payloadCaching = payloadCaching ;
		this.langAttributeOnSoap11FaultString = langAttributeOnSoap11FaultString ;
	}
	
	public SoapHeader getHeader(){
		
		try {
			if(getAxiomEnvelope().getHeader() == null){
				return null ;
			}
			else {
				SOAPHeader axiomHeader = getAxiomEnvelope().getHeader();
				String namespaceURI = getAxiomEnvelope().getNamespace().getNamespaceURI();
				if("http://schemas.xmlsoap.org/soap/envelope/".equals(namespaceURI)){
					return new AxiomSoap11Header(axiomHeader, getAxiomFactory());
				}
				else if("http://www.w3.org/2003/05/soap-envelope".equals(namespaceURI)){
					return new AxiomSoap12Header(axiomHeader, getAxiomFactory());
				}
				else {
					throw new AxiomSoapEnvelopeException("Unknown SOAP namespace \""+namespaceURI+"\"");
				}
			}
		} catch (OMException e) {
			throw new AxiomSoapHeaderException(e);
		}
		
	}
	
	public SoapBody getBody(){
		if(body == null){
			try {
				SOAPBody axiomBody = getAxiomEnvelope().getBody();//<soapenv:Body xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"></soapenv:Body>
				String namespaceURI = getAxiomEnvelope().getNamespace().getNamespaceURI();//http://schemas.xmlsoap.org/soap/envelope/
				if("http://schemas.xmlsoap.org/soap/envelope/".equals(namespaceURI)){// true
					body = new AxiomSoap11Body(axiomBody, getAxiomFactory(), payloadCaching, langAttributeOnSoap11FaultString);
				}
				else if("http://www.w3.org/2003/05/soap-envelope".equals(namespaceURI)){
					body = new AxiomSoap12Body(axiomBody, getAxiomFactory(), payloadCaching);
				}
				else {
					throw new AxiomSoapEnvelopeException("Unknown SOAP namespace \""+namespaceURI+"\"");
				}
			} catch (OMException e) {
				throw new AxiomSoapBodyException(e);
			}
		}
		return body ;
	}
	
	
	protected SOAPEnvelope getAxiomEnvelope(){
		return (SOAPEnvelope) getAxiomElement();
	}

}
